using UnityEngine;
using UnityEngine.Tilemaps;

namespace UndergroundSurvival.Core
{
    public class SceneSetup : MonoBehaviour
    {
        [Header("Tilemap Setup")]
        [SerializeField] private Grid mainGrid;
        [SerializeField] private Tilemap groundTilemap;
        [SerializeField] private Tilemap wallTilemap;
        [SerializeField] private Tilemap objectTilemap;
        [SerializeField] private Tilemap previewTilemap;

        [Header("Camera Setup")]
        [SerializeField] private Camera mainCamera;
        [SerializeField] private float cameraSpeed = 10f;
        [SerializeField] private float zoomSpeed = 2f;
        [SerializeField] private float minZoom = 2f;
        [SerializeField] private float maxZoom = 10f;

        private void Start()
        {
            SetupTilemaps();
            SetupCamera();
        }

        private void SetupTilemaps()
        {
            // 设置Tilemap的排序顺序
            groundTilemap.sortingOrder = 0;
            wallTilemap.sortingOrder = 1;
            objectTilemap.sortingOrder = 2;
            previewTilemap.sortingOrder = 3;

            // 设置Tilemap的颜色
            previewTilemap.color = new Color(1f, 1f, 1f, 0.5f);
        }

        private void SetupCamera()
        {
            if (mainCamera != null)
            {
                mainCamera.orthographic = true;
                mainCamera.orthographicSize = 5f;
            }
        }

        private void Update()
        {
            HandleCameraMovement();
            HandleCameraZoom();
        }

        private void HandleCameraMovement()
        {
            if (mainCamera == null) return;

            float horizontal = Input.GetAxis("Horizontal");
            float vertical = Input.GetAxis("Vertical");

            Vector3 movement = new Vector3(horizontal, vertical, 0) * cameraSpeed * Time.deltaTime;
            mainCamera.transform.position += movement;
        }

        private void HandleCameraZoom()
        {
            if (mainCamera == null) return;

            float scroll = Input.GetAxis("Mouse ScrollWheel");
            if (scroll != 0)
            {
                float newSize = mainCamera.orthographicSize - scroll * zoomSpeed;
                mainCamera.orthographicSize = Mathf.Clamp(newSize, minZoom, maxZoom);
            }
        }
    }
} 